package net.xyzmc.pokemonbroadcast.bukkit.commands;

import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import com.pixelmonmod.pixelmon.api.pokemon.PokemonSpec;
import com.pixelmonmod.pixelmon.enums.EnumSpecies;
import net.xyzmc.pokemonbroadcast.bukkit.Main;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.TabExecutor;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

public class PokemonBroadcastCommand implements TabExecutor {


    ///bc 宝可梦名称 true 30 第一行 第二行 第三行
    @Override
    public boolean onCommand(CommandSender commandSender, Command command, String s, String[] strings) {
        if (strings.length >= 4) {
            String pokemonName = strings[0];
            boolean isPokemon = false;
            for (EnumSpecies enumSpecies : EnumSpecies.values()) {
                if (enumSpecies.name.equalsIgnoreCase(pokemonName)) {
                    isPokemon = true;
                    break;
                }
            }
            if (isPokemon) {
                String shiny = Boolean.parseBoolean(strings[1]) ? "s" : "!s";
                PokemonSpec ps = PokemonSpec.from(pokemonName + " " + shiny);
                int time = Integer.parseInt(strings[2]);
                String[] broadcast = new String[strings.length - 3];
                System.arraycopy(strings, 3, broadcast, 0, strings.length - 3);
                Main.queue.offer(new Main.PokemonBroadcast(ps, time, broadcast));
                commandSender.sendMessage("§7消息已经加入队列");
                ByteArrayDataOutput out = ByteStreams.newDataOutput();
                out.writeUTF("Forward");
                out.writeUTF("ALL");
                out.writeUTF("PokeBroadcast");
                ByteArrayOutputStream msgbytes = new ByteArrayOutputStream();
                DataOutputStream msgout = new DataOutputStream(msgbytes);
                StringJoiner sj = new StringJoiner(" ");
                for (String str : broadcast) {
                    sj.add(str);
                }
                try {
                    String msg = pokemonName + " " + shiny + " " + time + " " + sj;
                    msgout.writeUTF(msg);
                    Main.messages.add(msg);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                out.writeShort(msgbytes.toByteArray().length);
                out.write(msgbytes.toByteArray());
                Main.instance.getServer().sendPluginMessage(Main.instance, "BungeeCord", out.toByteArray());
                return true;
            }
        }
        commandSender.sendMessage("§a/pokebc <宝可梦名称> <是否闪光true或false> <时间秒> <信息 ...> 发送宝可梦广播");
        return true;
    }

    @Override
    public List<String> onTabComplete(CommandSender commandSender, Command command, String s, String[] strings) {
        if (strings.length == 1) {
            List<String> list = new ArrayList<>();
            for (EnumSpecies enumSpecies : EnumSpecies.values()) {
                list.add(enumSpecies.name);
            }
            return filter(list, strings[strings.length - 1]);
        }
        if (strings.length == 2) {
            List<String> list = new ArrayList<>();
            list.add("true");
            list.add("false");
            return filter(list, strings[strings.length - 1]);
        }
        if (strings.length == 3) {
            List<String> list = new ArrayList<>();
            for (int i = 1; i <= 30; i++) {
                list.add(String.valueOf(i));
            }
            return filter(list, strings[strings.length - 1]);
        }
        return null;
    }

    private List<String> filter(List<String> list, String latest) {
        if (list.isEmpty() || latest == null) {
            return list;
        }
        String ll = latest.toLowerCase();
        list.removeIf(k -> !k.toLowerCase().startsWith(ll));
        return list;
    }

}
